<?php #title=Видео;

	$ids = array();
	
	if( isset( $_POST['save_video'] ) && isset( $_GET['id'] ) && $video_id = $_GET['id'] )
	{
		$conn->query('DELETE FROM `video_relation` WHERE video_id = '.$video_id );  // Сначала удаляем все связи с этим роликом
		if( isset( $_POST['cat'] ) )
			foreach( $_POST['cat'] as $cat_id )
				$ids[] = '('.$video_id.', "category", '.$cat_id.')';
				
		if( isset( $_POST['product'] ) )
			foreach( $_POST['product'] as $product_id )
				$ids[] = '('.$video_id.', "product", '.$product_id.')';

		if( count( $ids ) )
			$conn->query('INSERT INTO `video_relation` ( video_id, type, id ) VALUES '.implode( ',', $ids ) );
			
		// successRedirect(array('url'=>'?p=video'));
	}


	
	
		
	if( isset( $_GET['action'] ) )
	{
		$html = '';
		$view->css('video');
		$view->js('admin/video');
		
		$view->css('jQuery.Tree');
		$view->js('jQuery.Tree');
		
			// Выбрать все категории
		$conn->query('	SELECT p.id, p.templ, p.parents, p.title, vr.video_id FROM pages AS p 
						LEFT JOIN video_relation AS vr ON p.id = vr.id AND p.id = vr.id AND vr.type = "category" AND vr.video_id = '.$_GET['id'].'
						WHERE p.templ IN( "category", "sub_category" )');
		while( $row = $conn->fetch() )
		{
			if( $row->parents )
				$navi[$row->templ][( $row->parents ? $row->parents : 'n' )][] = $row;
			elseif( $row->id == 2 )
				$navi[$row->templ][( $row->parents ? $row->parents : 'n' )][] = $row;
		}
			
		// echo '<pre>';
		// print_r( $navi );
		
 		$conn->query('SELECT title FROM `video` WHERE id = '.$_GET['id'] );
		if( $row = $conn->fetch() )
			$html .= '<h2>'.$row->title.'</h2><br>';

			
		$html .= '<ul id="tree">';
		foreach( $navi['category']['n'] as $main_cat ) 
		{
			$html .= '<li><label><input name="cat[]" value="'.$main_cat->id.'" type="checkbox"'.( $main_cat->video_id ? ' checked' : '' ).'/>'.$main_cat->title.'</label>';
			$html .= getproductHtmlList( '#'.$main_cat->id.'#' );
			if( isset( $navi['sub_category']['#'.$main_cat->id.'#'] ) )
			{
				$html .= '<ul>';
				foreach( $navi['sub_category']['#'.$main_cat->id.'#'] as $cat )
				{
					$html .= '<li><label><input name="cat[]" value="'.$cat->id.'" type="checkbox"'.( $cat->video_id ? ' checked' : '' ).'/>'.$cat->title.'</label>';
					$html .= getproductHtmlList( $cat->parents.'#'.$cat->id.'#' );
					if( isset( $navi['sub_category']['#'.$main_cat->id.'##'.$cat->id.'#'] ) )
					{
/* 						$html .= '<ul>';
						foreach( $navi['sub_category']['#'.$main_cat->id.'##'.$cat->id.'#'] as $sub_cat )
						{
							$html .= '<li><label><input name="cat[]" value="'.$sub_cat->id.'" type="checkbox"'.( $sub_cat->video_id ? ' checked' : '' ).'/>'.$sub_cat->title.'</label>';
							$html .= getproductHtmlList( $sub_cat->parents.'#'.$sub_cat->id.'#' );
							$html .= '</li>';
						}
						$html .= '</ul>'; */
					}
					$html .= '</li>';
				}
				$html .= '</ul>';
			}
			$html .= '</li>';
		}
		$html .= '</ul>';
		
		$html = '
			<div action="" class="video">
				<form method="post">
					<p><input type="submit" name="save_video" value="Сохранить"/></p><br/>
					'.$html.'
				</form>
			</div>';
	}
	else
	{
		
		$html = '<p><h1>Редактор видео</h1></p>'.
			$load->block('table_editor',array(
				'control_name'	=> 'Быстрое редактирование',
				'can_edit'		=> true,
				'dev'			=> true,
				'select'		=> array('from' => 'video'),
				'delete'		=> array('DELETE FROM video WHERE ID IN(#sys_ids#)'),
				'insert'		=> 'INSERT INTO video SET title = "#title#", site = "#site#", code = "#code#", zin = #zin#',
				'update'		=> 'UPDATE video SET title = "#title#", site = "#site#", code = "#code#", zin = #zin# WHERE id = #sys_id#',
				'fields'		=> array(
					'id'		=> array('title' => 'ID', 'sys_id' => true, 'to_edit' => false ),
					'title'		=> array('title' => 'Название'),
					'site'		=> array('title' => 'Сайт', 'type' => 'select', 'values' => array( 'youtube' => 'Youtube', 'videobaker' => 'Videobaker' ) ),
				//	'name_sys'	=> array('title' => 'Системное название'),
					'code'		=> array('title' => 'ID видео'),
					'zin'		=> array('title' => 'Сорт.'),
				//	array('link'=>"<a href='?p=video&action=edit&id=#id#'>Редактировать</a>", 'to_edit'=>0),
				),
				'link_replaces'	=> array('parents')
			));
	}
	
	function getproductHtmlList( $parents )
	{
		$html = '';
		global $conn;
		static $all_products = array();
		
 		if( !count( $all_products ) )  // Выбрать все товары
		{
			$conn->query('	SELECT p.parents, p.brand, p.product_code, p.name, vr.video_id FROM `store_products_view` AS p
							LEFT JOIN video_relation AS vr ON p.product_code = vr.id AND vr.type = "product" AND vr.video_id = '.$_GET['id'].'
							ORDER BY p.brand, p.name');
			while( $row = $conn->fetch() )
				$all_products[$row->parents][$row->brand][$row->product_code] = array( 'name' => $row->name, 'video' => $row->video_id );
		}
		
 		if( isset( $all_products[$parents] ) )
		{
			$html .= '<ul>';
			foreach( $all_products[$parents] as $brand => $products )
			{
				$html .= '<li><label><input type="checkbox"/>'.$brand.'</label>';
 				$html .= '<ul>';
				foreach( $products as $product_code => $product )
				{
					$html .= '
						<li><label>
							<input name="product[]" value="'.$product_code.'" type="checkbox"'.( $product['video'] ? ' checked' : '' ).'/>
							'.$product_code.' <b>'.$product['name'].'</b>
							</label>
						</li>';
				}
				$html .= '</ul>';
				$html .= '</li>';
			}
			$html .= '</ul>';
		}
		return $html;
	}
	
	$view->gText .= $html;
